package com.frikinet.lifeguard.gui.dialogs;

import java.awt.BorderLayout;
import java.awt.Dimension;
import java.awt.GridLayout;
import java.io.File;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.event.ListSelectionEvent;
import javax.swing.event.ListSelectionListener;

import com.frikinet.lifeguard.App;
import com.frikinet.lifeguard.dao.PostImageDAO;
import com.frikinet.lifeguard.listeners.AppModelStateListener;
import com.frikinet.lifeguard.model.Post;
import com.frikinet.lifeguard.model.User;
import com.frikinet.lifeguard.services.ExportUserFiles;
import com.frikinet.lifeguard.util.HibernateUtil;
import com.frikinet.tools.gui.ActionHandler;
import com.frikinet.tools.gui.FButton;
import com.frikinet.tools.gui.FList;
import com.frikinet.tools.gui.FListCellRenderer;
import com.frikinet.tools.gui.FListModel;
import com.frikinet.tools.gui.FOptionPane;

public class UserAdminDialog extends JDialog implements ListSelectionListener, AppModelStateListener
{
	private static final long serialVersionUID = 1L;
	
	private ActionHandler actionHandler = new ActionHandler(this);
	private JFileChooser fileChooser = new JFileChooser();
	
	private UserList list;
	private Map<String, JButton> buttons = new HashMap<String, JButton>();

	public UserAdminDialog(JFrame parent)
	{
		super(parent);
		setTitle(App.i18n.translate("dialog.users.title"));
		setModal(true);
		
		buildGUI();
		
		setupInitialState();
		App.events.addModelStateListener(this);
		
		pack();
		setLocationRelativeTo(parent);
		setVisible(true);
	}
	
	private void buildGUI()
	{
		fileChooser.setFileSelectionMode(JFileChooser.FILES_ONLY);
		fileChooser.setMultiSelectionEnabled(false);
		
		JPanel contentPane = new JPanel(new BorderLayout(5,5));
		contentPane.setBorder(BorderFactory.createEmptyBorder(5, 5, 5, 5));
		
		list = new UserList();
		list.setPreferredSize(new Dimension(250, 1));
		list.addListSelectionListener(this);
		contentPane.add(list, BorderLayout.CENTER);
		
		String[] buttonNames = {"add", "update", "export", "remove"};
		JPanel buttons = new JPanel(new GridLayout(buttonNames.length, 1));
		for(String buttonName: buttonNames)
		{
			buttons.add(createButton(buttonName));
		}
		contentPane.add(buttons, BorderLayout.EAST);
		
		setContentPane(contentPane);
	}
	
	private JButton createButton(String name)
	{
		FButton button = new FButton(String.format("dialog.users.buttons.%s", name));
		button.setI18N(App.i18n);
		button.setActionCommand(name);
		button.addActionListener(actionHandler);
		button.translate();
		buttons.put(name, button);
		return button;
	}
	
	private void setupInitialState()
	{
		String[] select = {"update", "export", "remove"};
		for(String button: select)
		{
			buttons.get(button).setEnabled(false);
		}
	}
	
	public void action_add()
	{
		new FotologLoadingDataDialog(this, "");
		list.update();
	}
	
	public void action_update()
	{
		new FotologLoadingDataDialog(this, ((User)list.getSelectedValue()).getName());
	}
	
	public void action_export()
	{
		if(fileChooser.showSaveDialog(this) == JFileChooser.APPROVE_OPTION)
		{
			File target = fileChooser.getSelectedFile();
			if(FOptionPane.showConfirmOverwrite(this, App.i18n, "msg.file.overwrite", target))
			{
				try
				{
					new ExportUserFiles((User)list.getSelectedValue()).exportDataToZip(target);
				}
				catch (IOException e)
				{
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
	}
	
	public void action_remove()
	{
		User user = (User)list.getSelectedValue();
		if(FOptionPane.showConfirmDialog(this, App.i18n, "dialog.users.msg.remove", user.getName()))
		{
			for(Post post: user.getPosts())
			{
				PostImageDAO.delete(post.getUid());
			}
			HibernateUtil.delete(user);
			App.users.remove(user);
			App.events.fireModelChanged();
		}
	}

	@Override
	public void modelChanged()
	{
		list.update();
	}

	@Override
	public void valueChanged(ListSelectionEvent e)
	{
		String[] select = {"update", "export", "remove"};
		for(String button: select)
		{
			buttons.get(button).setEnabled(list.getSelectedIndex()!=-1);
		}
	}

	public class UserList extends FList
	{
		private static final long serialVersionUID = 1L;
		
		private class UserListModel extends FListModel
		{
			private static final long serialVersionUID = 1L;

			@Override
			public Object getElementAt(int index)
			{
				return App.users.toArray()[index];
			}

			@Override
			public int getSize()
			{
				return App.users.size();
			}
		}
		
		public class UserListRenderer extends FListCellRenderer<User>
		{
			private static final long serialVersionUID = 1L;

			@Override
			public String formatValue(User value)
			{
				return App.i18n.translate("dialog.users.list.display", value.getName(), value.getPosts().size());
			}
		}
		
		public UserList()
		{
			super();
			
			setModel(new UserListModel());
			setCellRenderer(new UserListRenderer());
		}
	}
}
